package com.sxc.study;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Create by sunxunchao on 2019/9/17
 */
public class LogAdapter extends RecyclerView.Adapter<LogAdapter.LogViewHolder> {

    private List<File> files = new ArrayList<>();

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void add(List<File> files) {
        this.files = files;
        this.notifyDataSetChanged();
    }

    @NonNull
    @Override
    public LogViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new LogViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.simple_list_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull LogViewHolder logViewHolder, final int i) {
        logViewHolder.tv.setText(files.get(i).getName());
        logViewHolder.tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClicked(files.get(i));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return files.size();
    }

    public interface OnItemClickListener {
        void onItemClicked(File file);
    }

    static class LogViewHolder extends RecyclerView.ViewHolder {

        private TextView tv;

        public LogViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
        }
    }
}
